<? 
require("adm_header.php"); 
writeCommonAdminTable(LA_ADMIN_CATEGORIES,1);
$res=q("select count(cat_id) as num_cat from $cat_tbl");
$row=mysql_fetch_array($res);
$num_cat=$row["num_cat"];
if ($num_cat==0 AND getParam("edit","")=="")
	redirect("adm_categories.php?edit=-1");
	
echo "<p><a href='?'><b>".LA_ADMIN_CATEGORIES."</b></a> | <a href='?edit=-1'>".LA_ADMIN_NEWCAT."</a></p>";
echo "<form method='post' action='adm_categories.php' enctype='multipart/form-data'>";

if ($set_cat_sort=="")
{
	echo "<p>WARNING: You have not followed the instructions to save 'Options' settings in admin area. Please do it now!</p>";
}

// Check if global table exist, which it should
$sql="select count(tplid) as num from $template_tbl";
$res_count=q($sql);
$row_c=mysql_fetch_array($res_count);
$num_tpl=$row_c["num"];

// Create global template
if ($num_tpl<1)
	q("INSERT INTO $template_tbl (`tplid`, `name`, `del`) VALUES (1, 'GLOBAL', NULL);");


$lHide=0;
$lCatName=getParam("catname","");
$lCatDescription=getParam("catdescription","");
$lCatAllowAds=getParam("allowads","");
$lCatFatherId=getParam("catfatherid","");
$lCatTpl=getParam("cattpl","");
$lCatImage=getParam("catimage","");
$lCatCost=getParam("lCatCost","0");

$cat_order_number=getParam("cat_order_number","99");
$lCatShowInFp=getParam("show_in_fp","0");



$lCharSetString="CHARACTER SET utf8 COLLATE utf8_unicode_ci";
$lVerArray=getMySQLVersion();

$lMajor=$lVerArray[0];
$lSub=$lVerArray[1];
if ($lMajor !=5)
{
	if ($lMajor==4 && $lSub==0)
		$lSkipUTF=1;
	if ($lMajor==3)
		$lSkipUTF=1;
}

if ($lSkipUTF==1)
	$lCharSetString="";

if (!$lSublinks_disp)
	$lSublinks_disp=0;
if (!$lSublinks_lim>0)
	$lSublinks_lim=3;
if (!$lCatFatherId>0)
	$lCatFatherId=0;

if ($set_multilang_cat)
{
	// Find what Multi Language Category to support. If column doesn't exists, create it.
	$lLangSupportedArray=split(",",$set_multilang_cat);
	$lLangInDatabaseNow=getFields($cat_tbl);
	
	foreach ($lLangInDatabaseNow as $lItem)
		$lLangInDbArray[]=$lItem["Field"];
	
	foreach ($lLangSupportedArray as $lItem)
	{
		$lLangToSupport="lang_" . $lItem;
		if (!in_array($lLangToSupport,$lLangInDbArray))
			q("ALTER TABLE `$cat_tbl` ADD $lLangToSupport VARCHAR( 100 ) $lCharSetString NULL ;");	
	}
}

	
if (getParam("delete","") && !$set_demo)
{
	if (getParam("delete_confirmed",""))
	{
		$ltmpChilds=getCategoryChildren(getParam("edit",""));
		
		if ($ltmpChilds)
		{
			$lAddOnQuery.=" AND ad_cat_id IN (". $ltmpChilds . ",". getParam("edit",0) .")";
			$lAddOnQueryCat.=" AND cat_id IN (". $ltmpChilds . ",". getParam("edit",0) .")";

		}
		else
		{
			$lAddOnQuery.=" AND ad_cat_id=". getParam("edit",0);
			$lAddOnQueryCat.=" AND cat_id=". getParam("edit",0);
		}
		
		$lSql_1="select * from $ads_tbl WHERE 1<2 $lAddOnQuery";
		$res_select=q($lSql_1);
		while ($row=mysql_fetch_array($res_select))
		{
			echo "delete " . $row["ad_id"] . "<br>";
			delete_ad($row["ad_id"]);
		}
		$lSql_1="delete from $cat_tbl WHERE 1<2 $lAddOnQueryCat";
		$res_select=q($lSql_1);
		echo "<p><b class='red'>Categories and ads was deleted</b></p>";
		$lHide=1;
	}
	else 
	{
		echo "<input type='hidden' name='delete' value='1'>";
		echo "<p><b class='red'>".LA_ADMIN_CAT_CONFIRM_DEL."</b></p>";
		echo "<p><input type='submit' class='red' name='delete_confirmed' value='".LA_ADMIN_CAT_DEL."'></p>";
	}
	
}
	
	
if (getParam("submit","") && !$set_demo)
{
	$data_cols="";
	$data="";
	$data_val="";
	
	if ($set_multilang_cat)
	{
		foreach ($lLangSupportedArray as $lItem)
		{
			$lLangToSupport="lang_" . $lItem;
			$data.=",$lLangToSupport='" . strToDb(getParam($lLangToSupport,'')) . "'";
			$data_cols.=",$lLangToSupport";
			$data_val.=",'" . strToDb(getParam($lLangToSupport,'')) . "'";
		}
	}
	
	$lCatName=strToDb($lCatName);
	$lCatDescription=strToDb($lCatDescription);
		
	if (getParam("edit","")==-1)
	{
		if (!$lCatCost)
			$lCatCost=0;
		$sql="insert into $cat_tbl (cat_name,cat_description,cat_image,cat_tpl,cat_allow_ads,cat_fatherid,cat_cost, show_in_frontpage, order_number $data_cols) values (
		'$lCatName', '$lCatDescription', '$lCatImage', '$lCatTpl','$lCatAllowAds',$lCatFatherId,$lCatCost, $lCatShowInFp, $cat_order_number $data_val)";
		$res=q($sql);
		$id=mysql_insert_id();
		echo "<b>".LA_ADMIN_CATCREATED."</b><br />";
		echo "<p>".LA_ADMIN_CAT."<i> " . strEnc($lCatName) . "</i> ";
		if ($lCatAllowAds!="on")
			echo " " . LA_ADMIN_CAT_NO_ADS;
		echo " ".LA_ADMIN_CAT_WAS_CREATED."</p>";

		echo "<p>".LA_ADMIN_CAT_OPTIONS."<ul type='square'><li><a href='?edit=$id'>".LA_ADMIN_CAT_OPTIONS_1."</a></li><li><a href='?edit=-1&catfatherid=$lCatFatherId'>".LA_ADMIN_CAT_OPTIONS_2."</a></li><li><a href='?edit=-1'>".LA_ADMIN_CAT_OPTIONS_3."</a></li></ul></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>";
		
	}
	elseif (getParam("edit","")>0)
	{
		$id=getParam("edit","");
		$lCatName=$_REQUEST["catname"];
		$sql="update $cat_tbl set cat_name='$lCatName',cat_description='$lCatDescription',cat_image='$lCatImage',cat_tpl='$lCatTpl',cat_allow_ads='$lCatAllowAds',cat_fatherid=$lCatFatherId,cat_cost='$lCatCost',show_in_frontpage='$lCatShowInFp', order_number='$cat_order_number' $data where cat_id=" . getParam("edit","");
		$res=q($sql);
		echo "<b>".LA_ADMIN_CATUPDATED."</b><br />";
		echo "<p>".LA_ADMIN_CAT." <i>". $lCatName . "</i> ";
		if ($lCatAllowAds!="on")
			echo " " . LA_ADMIN_CAT_NO_ADS;
		echo " ".LA_ADMIN_CAT_WAS_UPDATED."</p>";

		echo "<p>".LA_ADMIN_CAT_OPTIONS."<ul type='square'><li><a href='?edit=$id'>".LA_ADMIN_CAT_OPTIONS_1."</a></li><li><a href='?edit=-1&catfatherid=$lCatFatherId'>".LA_ADMIN_CAT_OPTIONS_2."</a></li><li><a href='?edit=-1'>".LA_ADMIN_CAT_OPTIONS_3."</a></li></ul></p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>";
	}
}


if (getParam("edit","") AND getParam("submit","")=="" AND $lHide!=1)
{
	echo "<input type='hidden' name='edit' value='".getParam("edit","")."'>";

	echo "<table width='100%' class='noBorder'><tr><td colspan='2'>";
	if (getParam("edit","")==-1)
		echo "<p><b>".LA_ADMIN_NEWCAT."</b><hr /></p>";
	elseif (getParam("edit","")>0)
	{
		echo "<p><b>".LA_ADMIN_EDITCAT."</b><hr /></p>";
		
		$sql="select * from $cat_tbl where cat_id=" . getParam("edit","");
		$res=q($sql);
		$crow=mysql_fetch_array($res);
		
		$lCatDescription=strEnc($crow["cat_description"]);
		$lCatName=strEnc($crow["cat_name"]);
		$lCatImage=$crow["cat_image"];
		$lCatFatherId=$crow["cat_fatherid"];
		$lCatAllowAds=$crow["cat_allow_ads"];
		$lCatTpl=$crow["cat_tpl"];
		$lCatCost=$crow["cat_cost"];
		$lCatTpl=$crow["cat_tpl"];
		$lCatShowInFp=$crow["show_in_frontpage"];
		$cat_order_number=$crow["order_number"];	
		
	}
	echo "</td></tr>";	

	$lStyleColor="";
	$lAllowads_checked="";
	$lSublinks_disp_checked="";
	$lCatShowInFp_checked="";

	if ($lCatShowInFp)
		$lCatShowInFp_checked="checked";
	
	if ($lCatAllowAds)
		$lAllowads_checked="checked";
	if ($lSublinks_disp)
		$lSublinks_disp_checked="checked";
	if (!$lCatImage)
		$lCatImage="default.gif";
		
	$lOrderBy="";	
	$sql = "SHOW FIELDS FROM $cat_tbl";
    $res = q($sql);
    while ($row=mysql_fetch_array($res))
    {	
    		$lOrderBy.= "<option";
    		if ($row['Field']==$lSublinks_ord)
    			$lOrderBy.=" selected";
    		$lOrderBy.= ">" . $row['Field'] . "</option>";
    }	
	
   
	$field_array[LA_ADMIN_CATEGORY_NAME]="<input type='text' value='$lCatName' name='catname' style='width:260px;'> <span class='red'>*</span>";
	$field_array[LA_ADMIN_CATEGORY_DESCR]="<textarea name='catdescription' cols='30' rows='3' style='width:260px;'>$lCatDescription</textarea>";
	$field_array[LA_ADMIN_CAT_BELOW]="<select name='catfatherid' style='width:266px;'><option value='0'>".LA_ADMIN_NONE."</option>".getCategoriesDD($lCatFatherId,"") . "</select> <span class='red'>*</span>";
	$field_array[LA_ADMIN_CAT_IMAGE]="<select onchange='previewImageServer(this.value)' name='catimage' style='width:266px;'>".getFileList("catimages",array("gif","jpg","png"),$lCatImage) . "</select><img  src='../layout_images/blank.gif' id='previewImage' hspace='10' height='15'>";
	$field_array[LA_ADMIN_CAT_TPL]="<select name='cattpl' style='width:266px;'>".getCategoryTemplates($lCatTpl) . "</select>";
	$field_array[LA_ADMIN_ALLOW_ADS]="<input type='checkbox' name='allowads' value='on' $lAllowads_checked> " . LA_ADMIN_YES;
	if ($set_payments==1)
		$field_array[LA_ADMIN_CAT_PRICE]="<input type='text' value='$lCatCost' name='lCatCost' size='1'>";
		
	$field_array['Mostrar En Pagina principal']="<input type='checkbox' name='show_in_fp' value='1' $lCatShowInFp_checked> " . LA_ADMIN_YES;
	$field_array['Nro de orden']="<input type='text' value='$cat_order_number' name='cat_order_number' size='3' maxlength='3'>";
	
	
	foreach ($field_array as $key => $value) {
		
		$color=colorSwitch($color);
		echo "<tr class='$color' $str><td valign='top'>$key</td><td>$value</td></tr>";
		
		
		
		
	}
	
	if ($lCatImage)
	{
		echo "<script language='javascript'>";
		echo "previewImageServer('../catimages/$lCatImage');";
		echo "</script>";
	}
	
	if ($set_multilang_cat)
	{
		
		// Loop through each Multi Language column created
		foreach ($lLangSupportedArray as $lItem)
		{
			$lLangToSupport="lang_" . $lItem;
			if ($lStyleColor=="rowColor1")
				$lStyleColor="rowColor2";
			else if ($lStyleColor=="rowColor2") 
				$lStyleColor="rowColor1";
			else 
				$lStyleColor="rowColor2";
			
			echo "<tr style=\"$lStyleColor\">";
			echo "<td valign=\"top\"> ".LA_ADMIN_CATEGORY_NAME." $lItem</td>";
			echo "<td valign=\"top\">";
			echo "<input type=\"text\" name=\"$lLangToSupport\" style='width:260px;' value='".$crow["$lLangToSupport"]."'>";
			echo "</td>";
			echo "</tr>";
			
  		}
  	}
	
	
	echo "<tr><td colspan='2'><br />".LA_ADMIN_CAT_OPTIONS.":<br><a href='adm_set.php?file=opt_inc.php#set_yahoostyle_cat'>Yahoo style cat</a> | <a href='adm_set.php?file=pay_inc.php#set_pay'>Category Payment</a> | <a href='adm_set.php?file=opt_inc.php#set_multilang_cat'>Multi Language categories</a>.</td></tr>";

	echo "<tr><td><p>&nbsp;</p><p><input type='submit' name='submit' value='Save category'></p></td><td><p>&nbsp;</p><p><input type='submit' class='red' name='delete' value='".LA_ADMIN_CAT_DEL."'></p></td></tr>";
	echo "</table>";
}
elseif (getParam("edit","")=="") 
{
	
	echo "<b>".LA_ADMIN_QUICK_EDIT."</b><br /><select name='edit'>" . getCategoriesDD("","") . "</select>&nbsp;<input type='submit' name='sb' value='".LA_ADMIN_EDIT."'>\n";
	echo "<br /><br /><table width='100%' class='noBorder'><tr class='trHeadLine'><td><b>".LA_ADMIN_CATEGORIES."</b></td><td><b>".LA_ADMIN_NUM_ADS."</b></td><td><b>".LA_ADMIN_ALLOW_ADS."</b></td><td><b>".LA_ADMIN_ACTION."</b></td></tr>";
	
	echo flat_getCategoriesDD("","");
	
	echo "</table>";
}
echo "</form>";
writeCommonAdminTable("","");
require("adm_footer.php"); 
?>